package cn.edu.neusoft.practice.Controller;

import io.swagger.annotations.Api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


//import cn.edu.neusoft.practice.common.Result;
import cn.edu.neusoft.practice.mapper.DoctorWorkMapper;
import cn.edu.neusoft.practice.mapper.PatientDoctorMapper;
import cn.edu.neusoft.practice.po.DoctorWork;
import cn.edu.neusoft.practice.po.PatientDoctor;
import cn.edu.neusoft.practice.po.Personage;

@CrossOrigin
@Api("PatientDoctorController相关的api")
@RestController
public class PatientDoctorController {
    @Autowired
    PatientDoctorMapper patientDoctorMapper;
    @Autowired
    DoctorWorkMapper doctorWorkMapper;
    @RequestMapping("/addPatientDoctor")
    @ResponseBody
    public int addpatient(PatientDoctor patient){
    	DoctorWork dw=new DoctorWork();
    	dw.setDay(patient.getDay());
    	dw.setDocId(patient.getDocId());   	
    	if(doctorWorkMapper.updateCountLeftById(dw)>0)
    		return patientDoctorMapper.insert(patient);
    	else
    		return -1;//没有剩余的票
    }
    @GetMapping("/getPersonage")
    @ResponseBody
    public List<Personage> getpersonage(Personage personage){
        List<Personage> list = patientDoctorMapper.getpersonage(personage);
        //return Result.ok(list);
        return list;
    }
    
    @GetMapping("/deletePatientDoctor")
    @ResponseBody
    public int deletePatientDoctor(PatientDoctor doctor){
    	doctor=patientDoctorMapper.selectById(doctor.getId());
    	DoctorWork dw=new DoctorWork();
    	dw.setDay(doctor.getDay());
    	dw.setDocId(doctor.getDocId());   
    	doctorWorkMapper.updateCountLeftById2(dw);
        int i = patientDoctorMapper.deleteById(doctor.getId());
        if(i>0){
            //return Result.ok("删除成功");
            return 1;
        }else
            //return Result.fail("删除失败");
        	return -1;
    }
}
